package com.qf.controller;

import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * --- 天道酬勤 ---
 *
 * @author 卢泓光
 * @date 2025/8/8
 * @desc
 */
@Controller
@RestController
public class DemoController {
    @Autowired
    private EmpService empService;
//实现emp的增加员工操作
    @GetMapping("/emp/add")
    public R addEmp(Emp emp){
      int i  =  empService.addEmp(emp);
      if (i>0){
          return R.success("增加员工成功");
      }else{
          return R.fail(5000,"增加员工失败");
      }
    }

    //实现根据id删除员工信息
    @GetMapping("emp/delect")
    public R delectEmp(Integer empno){
        int i = empService.delectEmp(empno);
        if (i>0){
            return R.success("删除员工成功");
        }else{
            return R.fail(5000,"删除员工失败");
        }
    }
    //实现员工信息的修改
    @GetMapping("emp/update")
    public R updateEmp(Emp emp){
        int i = empService.updateEmp(emp);
        if (i>0){
            return R.success("修改员工成功");
        }else{
            return R.fail(5000,"修改员工失败");
        }
    }
    //实现根据id查询员工操作
    @GetMapping("emp/query")
    public R queryEmp(Integer empno){
        Emp emp = empService.queryEmp(empno);
        if (emp!=null){
            return R.success(emp);
        }else{
            return R.fail(5000,"查询员工失败");
        }
    }
    //根据名字模糊查询
    @GetMapping("emp/queryLike")
    public R queryLike(String ename){
        List<Emp> emps = empService.queryLike(ename);
        if (emps!=null){
            return R.success(emps);
        }else{
            return R.fail();
        }
    }
    //路径参数查询
    @GetMapping("emp/query/{empno}")
    public R query(@PathVariable("empno") Integer empno){
        Emp emp = empService.queryEmp(empno);
        if (emp!=null){
            return R.success(emp);
        }else{
            return R.fail();
        }
    }
    //使用map集合根据名字和工作模糊查询
    @GetMapping("emp/mapQuery")
    public R mapQuery(@RequestParam Map<String,Object> map){
        List<Emp> emps = empService.queryLikes(map);
        if (emps!=null){
            return R.success(emps);
        }else{
            return R.fail();
        }
    }



}
